package cn.ycl.auth.biz.usercenter.controller;

import cn.ycl.auth.biz.usercenter.entity.Role;
import cn.ycl.auth.biz.usercenter.service.RoleService;
import cn.ycl.constans.Result;
import cn.ycl.syslog.OperationEnum;
import cn.ycl.syslog.SystemLog;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 角色信息表(Role)表控制层
 *
 * @author makejava
 * @author yuanchangliang
 * @since 2020-12-04 11:31:28
 * @since 2021-10-13 16:08:25
 */
@RestController
@RequestMapping("/role")
public class RoleController {
    /**
     * 服务对象
     */
    @Resource
    private RoleService roleService;


    /**
     * 通过用户编号查询角色列表
     * 获取前10条数据
     *
     * @param userId 主键
     * @return 单条数据
     */
    @GetMapping("/selectRoleByUserId/{userId}")
    public List<Role> selectRoleByUserId(@PathVariable("userId") Long userId) {
        List<Long> longs = roleService.selectRoleIdsByUserId(userId);
        ArrayList<Role> roles = new ArrayList<>();
        for (Long aLong : longs) {
            roles.add(roleService.queryById(aLong));
        }
        return roles;
    }

    @GetMapping("selectAll")
    public List<Role> selectAll() {
        return this.roleService.queryAllByLimit(0, 10);
    }

    /**
     * 通过实体作为筛选条件查询
     *
     * @param role 实例对象
     * @return 对象列表
     */
    @SystemLog(value = "进入角色管理", type = OperationEnum.ROLE_MANAGE)
    @GetMapping("/queryByRole")
    public PageInfo<Role> queryByUser(Role role) {
        //在下面两行代码尽量凑在一起，别分开
        PageHelper.startPage(role.getPageNum(), role.getPageSize());
        List<Role> roles = roleService.queryByRole(role);
        return new PageInfo<>(roles);
    }

    /**
     * 删除角色（逻辑删除）
     *
     * @param roleId 实例对象
     * @return 对象列表
     */
    @SystemLog(value = "删除角色", type = OperationEnum.ROLE_MANAGE)
    @PostMapping("/deleteRole/{roleId}")
    public Result deleteRole(@PathVariable("roleId") Long roleId) {
       boolean result = roleService.deleteById(roleId);
       return Result.ok(result);
    }

    /**
     * 添加角色
     *
     * @param role 实例对象
     * @return 对象列表
     */
    @SystemLog(value = "添加角色", type = OperationEnum.ROLE_MANAGE)
    @PostMapping("/insertRole")
    public Result insertRole(@RequestBody Role role) {
        if(Objects.isNull(role)){
            return Result.fail();
        }
        role.setRoleSort(2);
        role.setRoleType(1);
        return Result.ok(roleService.insert(role));
    }

    /**
     * 获取单个角色信息
     *
     * @param roleId 角色编号
     * @return 对象列表
     */
    @GetMapping("/selectOne/{roleId}")
    public Role selectOne(@PathVariable("roleId") Long roleId) {
        return roleService.queryById(roleId);
    }

    public static void main(String[] args) {
        String a = "300203,3031,3016,303017,300205,3007,303013,3024,303012,300403,3012,3023,3029,3025,3015,303016,3021,303010,303011,300101,303006,300201,3020,3009,3010,300208,3014,300401,3028,3022,300406,3017,303014,3002,3027,303001,300404,303003,300102,300209,300202,300204,300405,303009,300103,3008,303007,3030,300207,3001,303002,300402,3003,303008,3026,3011,303005,3004,301602,3018,301601,303004,300206,303015";
        String[] split = a.split(",");
        ArrayList<Integer> integers = new ArrayList<>();
        Arrays.stream(split).forEach(split1 -> integers.add(Integer.parseInt(split1)));
        Collections.sort(integers);
        for (Integer integer : integers) {
            System.out.println("\n" + integer);
        }
    }
}

